package org.jsrml.core.purchaser.entity;

import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicUpdate;
import org.jsrml.common.persistence.StringIdBaseEntity;
import org.jsrml.common.util.UUIDGenerator;
import org.jsrml.core.base.entity.BaseStatus;
import org.jsrml.core.base.entity.Role;
import org.jsrml.core.purchaser.command.SaveWorkerCo;

@DynamicUpdate
@Entity
@Table(name = M.TABLE_PREFIX + "WORKER")
public class Worker extends StringIdBaseEntity {

	private WorkerInfo baseInfo;

	@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
	private Set<Role> roles;

	/**
	 * 公司
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "COMPANY_ID", foreignKey = @ForeignKey(name = "none"))
	private Company company;

	/**
	 * 部门
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "DEPARTMENT_ID", foreignKey = @ForeignKey(name = "none"))
	private Department department;

	/**
	 * 职务
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "POSITIONS_ID", foreignKey = @ForeignKey(name = "none"))
	private Positions positions;

	private WorkerStatus workerStatus;

	private BaseStatus status;

	public void create(SaveWorkerCo command, Set<Role> roles, Company company, Department department,
			Positions positions) {

		setId(UUIDGenerator.getUUID());
		setBaseInfo(new WorkerInfo());

		getBaseInfo().setName(command.getName());
		getBaseInfo().setTelephone(command.getTelephone());
		getBaseInfo().setType(command.getType());
		setRoles(roles);
		setCompany(company);
		setDepartment(department);
		setPositions(positions);

		setStatus(new BaseStatus());
		getStatus().setRemove(false);
		getStatus().setCreateDate(new Date());
		getStatus().setCreateBy(command.getHead().getCurrentUser());

	}

	public void modify(SaveWorkerCo command, Set<Role> roles, Company company, Department department,
			Positions positions) {

		getBaseInfo().setName(command.getName());
		getBaseInfo().setTelephone(command.getTelephone());
		setRoles(roles);

		setCompany(company);
		setDepartment(department);
		setPositions(positions);

		if (getStatus() == null) {
			setStatus(new BaseStatus());
		}
		getStatus().setUpdateDate(new Date());
		getStatus().setUpdateBy(command.getHead().getCurrentUser());
	}

	public void remove() {
		getStatus().setRemove(true);
	}

	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	public BaseStatus getStatus() {
		return status;
	}

	public void setStatus(BaseStatus status) {
		this.status = status;
	}

	public WorkerInfo getBaseInfo() {
		return baseInfo;
	}

	public void setBaseInfo(WorkerInfo baseInfo) {
		this.baseInfo = baseInfo;
	}

	public WorkerStatus getWorkerStatus() {
		return workerStatus;
	}

	public void setWorkerStatus(WorkerStatus workerStatus) {
		this.workerStatus = workerStatus;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public Positions getPositions() {
		return positions;
	}

	public void setPositions(Positions positions) {
		this.positions = positions;
	}

}
